package com.core.tool.generate;

import java.util.*;
import java.lang.reflect.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class GenerateTool {
	private static Log log = LogFactory.getLog(GenerateTool.class);

	private ServiceGenerate serviceGenerate;
	private ActionGenerate actionGenerate;
	private DaoGenerate daoGenerate;
	private String pack;
	private String folder;

	/**
	 * 
	 * @param clazz
	 *            类名
	 * @param logicPack
	 *            模块逻辑包名
	 */
	public void autoGenerateClass(String clazz, String logicPack) {
		try {

			String objectname = clazz.substring(0, 1).toLowerCase()
					+ clazz.substring(1);

			Class instance = Class
					.forName(pack + "." + logicPack + "." + clazz);
			Field[] fields = instance.getDeclaredFields();

			List<FieldProperty> fieldProperties = new ArrayList<FieldProperty>();

			if (fields != null && fields.length > 0) {
				for (Field field : fields) {
					FieldProperty fp = new FieldProperty();
					fp.setName(field.getName());
					fp.setType(field.getType().toString().length() > 6 ? field
							.getType().toString().substring(6) : field
							.getType().toString());

					String fname = field.getName();
					fname = fname.substring(0, 1).toUpperCase()
							+ fname.substring(1);
					fp.setFname(fname);

					fieldProperties.add(fp);
				}
			}
			this.serviceGenerate.generateService(logicPack, clazz, objectname,fieldProperties, folder);
			this.actionGenerate.generateAction(logicPack, clazz, objectname,folder);
			this.daoGenerate.generateDao(logicPack, clazz, objectname,folder);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getPack() {
		return pack;
	}

	public void setPack(String pack) {
		this.pack = pack;
	}

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public ActionGenerate getActionGenerate() {
		return actionGenerate;
	}

	public void setActionGenerate(ActionGenerate actionGenerate) {
		this.actionGenerate = actionGenerate;
	}

	public ServiceGenerate getServiceGenerate() {
		return serviceGenerate;
	}

	public void setServiceGenerate(ServiceGenerate serviceGenerate) {
		this.serviceGenerate = serviceGenerate;
	}

	public void setDaoGenerate(DaoGenerate daoGenerate) {
		this.daoGenerate = daoGenerate;
	}

}
